package com.ticket.system.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.ticket.common.exception.base.BaseException;
import com.ticket.system.domain.TYanchuReservedTicket;
import com.ticket.system.mapper.TYanchuReservedTicketMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ticket.system.mapper.TYanchuReservedSeatsMapper;
import com.ticket.system.domain.TYanchuReservedSeats;
import com.ticket.system.service.ITYanchuReservedSeatsService;
import org.springframework.util.CollectionUtils;

/**
 * 演出预留座Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-10-17
 */
@Service
public class TYanchuReservedSeatsServiceImpl implements ITYanchuReservedSeatsService 
{
    @Autowired
    private TYanchuReservedSeatsMapper tYanchuReservedSeatsMapper;
    @Autowired
    private TYanchuReservedTicketMapper tYanchuReservedTicketMapper;

    /**
     * 查询演出预留座
     * 
     * @param id 演出预留座主键
     * @return 演出预留座
     */
    @Override
    public TYanchuReservedSeats selectTYanchuReservedSeatsById(Long id)
    {
        return tYanchuReservedSeatsMapper.selectTYanchuReservedSeatsById(id);
    }

    /**
     * 查询演出预留座列表
     * 
     * @param tYanchuReservedSeats 演出预留座
     * @return 演出预留座
     */
    @Override
    public List<TYanchuReservedSeats> selectTYanchuReservedSeatsList(TYanchuReservedSeats tYanchuReservedSeats)
    {
        return tYanchuReservedSeatsMapper.selectTYanchuReservedSeatsList(tYanchuReservedSeats);
    }

    /**
     * 新增演出预留座
     * 
     * @param tYanchuReservedSeats 演出预留座
     * @return 结果
     */
    @Override
    public int insertTYanchuReservedSeats(TYanchuReservedSeats tYanchuReservedSeats)
    {
        //校验本剧院是否存在座位名称和类型
        if (this.checkReservedName(null,tYanchuReservedSeats.getOperaId(),tYanchuReservedSeats.getReservedName())){
            throw new BaseException("预留座名称已存在");
        }
        if (this.checkReservedType(null,tYanchuReservedSeats.getOperaId(),tYanchuReservedSeats.getReservedType())){
            throw new BaseException("预留座类型已存在");
        }
        //默认新增时 预留座状态为开启状态
        tYanchuReservedSeats.setStatue(1);
        return tYanchuReservedSeatsMapper.insertTYanchuReservedSeats(tYanchuReservedSeats);
    }

    /**
     * 校验预留座类型是否存在
     *
     * @param id
     * @param operaId
     * @param reservedType
     * @return
     */
    private Boolean checkReservedType(Long id, Long operaId, String reservedType) {
        if (null != id){
            TYanchuReservedSeats seat = tYanchuReservedSeatsMapper.selectTYanchuReservedSeatsById(id);
            if (reservedType.equals(seat.getReservedType())){
                return false;
            }
        }
        TYanchuReservedSeats seats = new TYanchuReservedSeats();
        seats.setOperaId(operaId);
        List<TYanchuReservedSeats> tYanchuReservedSeats = tYanchuReservedSeatsMapper.selectTYanchuReservedSeatsList(seats);
        if (CollectionUtils.isEmpty(tYanchuReservedSeats)){
            return false;
        }
        List<String> collect = tYanchuReservedSeats.stream().map(TYanchuReservedSeats::getReservedType).collect(Collectors.toList());
        if (collect.contains(reservedType)){
            return true;
        }
        return false;
    }

    /**
     * 校验预留座名称是否存在
     *
     * @param id
     * @param operaId
     * @param reservedName
     * @return
     */
    private Boolean checkReservedName(Long id,Long operaId, String reservedName) {
        if (null != id){
            TYanchuReservedSeats seat = tYanchuReservedSeatsMapper.selectTYanchuReservedSeatsById(id);
            if (reservedName.equals(seat.getReservedName())){
                return false;
            }
        }
        TYanchuReservedSeats seats = new TYanchuReservedSeats();
        seats.setOperaId(operaId);
        List<TYanchuReservedSeats> tYanchuReservedSeats = tYanchuReservedSeatsMapper.selectTYanchuReservedSeatsList(seats);
        if (CollectionUtils.isEmpty(tYanchuReservedSeats)){
            return false;
        }
        List<String> collect = tYanchuReservedSeats.stream().map(TYanchuReservedSeats::getReservedName).collect(Collectors.toList());
        if (collect.contains(reservedName)){
            return true;
        }
        return false;
    }

    /**
     * 修改演出预留座
     * 
     * @param tYanchuReservedSeats 演出预留座
     * @return 结果
     */
    @Override
    public int updateTYanchuReservedSeats(TYanchuReservedSeats tYanchuReservedSeats)
    {
        //判断是否存在当前预留座与座位关联
        TYanchuReservedTicket ticket = new TYanchuReservedTicket();
        ticket.setResSeatId(tYanchuReservedSeats.getId());
        List<TYanchuReservedTicket> tickets = tYanchuReservedTicketMapper.selectTYanchuReservedTicketList(ticket);
        if (!CollectionUtils.isEmpty(tickets)){
            throw new BaseException("当前预留座类型存在与座位关联,不可编辑");
        }
        if (this.checkReservedName(tYanchuReservedSeats.getId(),tYanchuReservedSeats.getOperaId(),tYanchuReservedSeats.getReservedName())){
            throw new BaseException("预留座名称已存在");
        }
        if (this.checkReservedType(tYanchuReservedSeats.getId(),tYanchuReservedSeats.getOperaId(),tYanchuReservedSeats.getReservedType())){
            throw new BaseException("预留座类型已存在");
        }
        return tYanchuReservedSeatsMapper.updateTYanchuReservedSeats(tYanchuReservedSeats);
    }

    /**
     * 批量删除演出预留座
     * 
     * @param ids 需要删除的演出预留座主键
     * @return 结果
     */
    @Override
    public int deleteTYanchuReservedSeatsByIds(Long[] ids)
    {
        return tYanchuReservedSeatsMapper.deleteTYanchuReservedSeatsByIds(ids);
    }

    /**
     * 删除演出预留座信息
     * 
     * @param id 演出预留座主键
     * @return 结果
     */
    @Override
    public int deleteTYanchuReservedSeatsById(Long id)
    {
        //判断是否存在当前预留座与座位关联
        TYanchuReservedTicket ticket = new TYanchuReservedTicket();
        ticket.setResSeatId(id);
        List<TYanchuReservedTicket> tickets = tYanchuReservedTicketMapper.selectTYanchuReservedTicketList(ticket);
        if (!CollectionUtils.isEmpty(tickets)){
            throw new BaseException("当前预留座类型存在与座位关联,不可删除");
        }
        return tYanchuReservedSeatsMapper.deleteTYanchuReservedSeatsById(id);
    }
}
